
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Decorators · TypeScript Handbook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Patrick Zhong">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Mixins.html" />
    
    
    <link rel="prev" href="JSX.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="tutorials/">
            
                <a href="tutorials/">
            
                    
                    快速上手
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="tutorials/TypeScript in 5 minutes.html">
            
                <a href="tutorials/TypeScript in 5 minutes.html">
            
                    
                    5分钟了解TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="tutorials/ASP.NET Core.html">
            
                <a href="tutorials/ASP.NET Core.html">
            
                    
                    ASP.NET Core
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="tutorials/ASP.NET 4.html">
            
                <a href="tutorials/ASP.NET 4.html">
            
                    
                    ASP.NET 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="tutorials/Gulp.html">
            
                <a href="tutorials/Gulp.html">
            
                    
                    Gulp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="tutorials/Knockout.html">
            
                <a href="tutorials/Knockout.html">
            
                    
                    Knockout.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="tutorials/React & Webpack.html">
            
                <a href="tutorials/React & Webpack.html">
            
                    
                    React与webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="tutorials/React.html">
            
                <a href="tutorials/React.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="tutorials/Angular 2.html">
            
                <a href="tutorials/Angular 2.html">
            
                    
                    Angular 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="tutorials/Migrating from JavaScript.html">
            
                <a href="tutorials/Migrating from JavaScript.html">
            
                    
                    从JavaScript迁移到TypeScript
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    手册
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Basic Types.html">
            
                <a href="Basic Types.html">
            
                    
                    基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="Variable Declarations.html">
            
                <a href="Variable Declarations.html">
            
                    
                    变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="Interfaces.html">
            
                <a href="Interfaces.html">
            
                    
                    接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="Classes.html">
            
                <a href="Classes.html">
            
                    
                    类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="Functions.html">
            
                <a href="Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="Generics.html">
            
                <a href="Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="Enums.html">
            
                <a href="Enums.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="Type Inference.html">
            
                <a href="Type Inference.html">
            
                    
                    类型推论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="Type Compatibility.html">
            
                <a href="Type Compatibility.html">
            
                    
                    类型兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="Advanced Types.html">
            
                <a href="Advanced Types.html">
            
                    
                    高级类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="Utilit Types.md">
            
                <span>
            
                    
                    实用工具类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="Symbols.html">
            
                <a href="Symbols.html">
            
                    
                    Symbols
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="Iterators and Generators.html">
            
                <a href="Iterators and Generators.html">
            
                    
                    Iterators 和 Generators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="Modules.html">
            
                <a href="Modules.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="Namespaces.html">
            
                <a href="Namespaces.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="Namespaces and Modules.html">
            
                <a href="Namespaces and Modules.html">
            
                    
                    命名空间和模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="Module Resolution.html">
            
                <a href="Module Resolution.html">
            
                    
                    模块解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="Declaration Merging.html">
            
                <a href="Declaration Merging.html">
            
                    
                    声明合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="Writing Definition Files.html">
            
                <a href="Writing Definition Files.html">
            
                    
                    书写.d.ts文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="JSX.html">
            
                <a href="JSX.html">
            
                    
                    JSX
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.21" data-path="Decorators.html">
            
                <a href="Decorators.html">
            
                    
                    Decorators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="Mixins.html">
            
                <a href="Mixins.html">
            
                    
                    混入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="Triple-Slash Directives.html">
            
                <a href="Triple-Slash Directives.html">
            
                    
                    三斜线指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="Type Checking JavaScript Files.html">
            
                <a href="Type Checking JavaScript Files.html">
            
                    
                    JavaScript文件里的类型检查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="declaration files/Introduction.html">
            
                <a href="declaration files/Introduction.html">
            
                    
                    如何书写声明文件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="declaration files/Library Structures.html">
            
                <a href="declaration files/Library Structures.html">
            
                    
                    结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="declaration files/Do's and Don'ts.html">
            
                <a href="declaration files/Do's and Don'ts.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="declaration files/By Example.html">
            
                <a href="declaration files/By Example.html">
            
                    
                    举例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="declaration files/Deep Dive.html">
            
                <a href="declaration files/Deep Dive.html">
            
                    
                    深入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="declaration files/Publishing.html">
            
                <a href="declaration files/Publishing.html">
            
                    
                    发布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="declaration files/Consumption.html">
            
                <a href="declaration files/Consumption.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    工程配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="tsconfig.json.html">
            
                <a href="tsconfig.json.html">
            
                    
                    tsconfig.json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Project References.html">
            
                <a href="Project References.html">
            
                    
                    工程引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Typings for NPM Packages.html">
            
                <a href="Typings for NPM Packages.html">
            
                    
                    NPM包的类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Compiler Options.html">
            
                <a href="Compiler Options.html">
            
                    
                    编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Configuring Watch.html">
            
                <a href="Configuring Watch.html">
            
                    
                    配置 Watch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="Compiler Options in MSBuild.html">
            
                <a href="Compiler Options in MSBuild.html">
            
                    
                    在MSBuild里使用编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="Integrating with Build Tools.html">
            
                <a href="Integrating with Build Tools.html">
            
                    
                    与其它构建工具整合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="Nightly Builds.html">
            
                <a href="Nightly Builds.html">
            
                    
                    使用TypeScript的每日构建版本
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../wiki/">
            
                <a href="../wiki/">
            
                    
                    Wiki
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../wiki/this-in-TypeScript.html">
            
                <a href="../wiki/this-in-TypeScript.html">
            
                    
                    TypeScript里的this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../wiki/coding_guidelines.html">
            
                <a href="../wiki/coding_guidelines.html">
            
                    
                    编码规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../wiki/Common Errors.html">
            
                <a href="../wiki/Common Errors.html">
            
                    
                    常见编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../wiki/TypeScript-Editor-Support.html">
            
                <a href="../wiki/TypeScript-Editor-Support.html">
            
                    
                    支持TypeScript的编辑器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                <a href="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                    
                    结合ASP.NET v5使用TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../wiki/Architectural-Overview.html">
            
                <a href="../wiki/Architectural-Overview.html">
            
                    
                    架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../wiki/Roadmap.html">
            
                <a href="../wiki/Roadmap.html">
            
                    
                    发展路线图
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../release-notes/">
            
                <a href="../release-notes/">
            
                    
                    新增功能
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../release-notes/TypeScript 3.1.html">
            
                <a href="../release-notes/TypeScript 3.1.html">
            
                    
                    TypeScript 3.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../release-notes/TypeScript 3.0.html">
            
                <a href="../release-notes/TypeScript 3.0.html">
            
                    
                    TypeScript 3.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../release-notes/TypeScript 2.9.html">
            
                <a href="../release-notes/TypeScript 2.9.html">
            
                    
                    TypeScript 2.9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../release-notes/TypeScript 2.8.html">
            
                <a href="../release-notes/TypeScript 2.8.html">
            
                    
                    TypeScript 2.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../release-notes/TypeScript 2.7.html">
            
                <a href="../release-notes/TypeScript 2.7.html">
            
                    
                    TypeScript 2.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../release-notes/TypeScript 2.6.html">
            
                <a href="../release-notes/TypeScript 2.6.html">
            
                    
                    TypeScript 2.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../release-notes/TypeScript 2.5.html">
            
                <a href="../release-notes/TypeScript 2.5.html">
            
                    
                    TypeScript 2.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../release-notes/TypeScript 2.4.html">
            
                <a href="../release-notes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../release-notes/TypeScript 2.3.html">
            
                <a href="../release-notes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../release-notes/TypeScript 2.2.html">
            
                <a href="../release-notes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.11" data-path="../release-notes/TypeScript 2.1.html">
            
                <a href="../release-notes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.12" data-path="../release-notes/TypeScript 2.0.html">
            
                <a href="../release-notes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.13" data-path="../release-notes/TypeScript 1.8.html">
            
                <a href="../release-notes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.14" data-path="../release-notes/TypeScript 1.7.html">
            
                <a href="../release-notes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.15" data-path="../release-notes/TypeScript 1.6.html">
            
                <a href="../release-notes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.16" data-path="../release-notes/TypeScript 1.5.html">
            
                <a href="../release-notes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.17" data-path="../release-notes/TypeScript 1.4.html">
            
                <a href="../release-notes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.18" data-path="../release-notes/TypeScript 1.3.html">
            
                <a href="../release-notes/TypeScript 1.3.html">
            
                    
                    TypeScript 1.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.19" data-path="../release-notes/TypeScript 1.1.html">
            
                <a href="../release-notes/TypeScript 1.1.html">
            
                    
                    TypeScript 1.1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../breaking-changes/breaking-changes.html">
            
                <a href="../breaking-changes/breaking-changes.html">
            
                    
                    Breaking Changes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../breaking-changes/TypeScript 2.4.html">
            
                <a href="../breaking-changes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../breaking-changes/TypeScript 2.3.html">
            
                <a href="../breaking-changes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../breaking-changes/TypeScript 2.2.html">
            
                <a href="../breaking-changes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../breaking-changes/TypeScript 2.1.html">
            
                <a href="../breaking-changes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../breaking-changes/TypeScript 2.0.html">
            
                <a href="../breaking-changes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../breaking-changes/TypeScript 1.8.html">
            
                <a href="../breaking-changes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../breaking-changes/TypeScript 1.7.html">
            
                <a href="../breaking-changes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../breaking-changes/TypeScript 1.6.html">
            
                <a href="../breaking-changes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../breaking-changes/TypeScript 1.5.html">
            
                <a href="../breaking-changes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="../breaking-changes/TypeScript 1.4.html">
            
                <a href="../breaking-changes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Decorators</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4ECB;&#x7ECD;">&#x4ECB;&#x7ECD;</h1>
<p>&#x968F;&#x7740;TypeScript&#x548C;ES6&#x91CC;&#x5F15;&#x5165;&#x4E86;&#x7C7B;&#xFF0C;&#x5728;&#x4E00;&#x4E9B;&#x573A;&#x666F;&#x4E0B;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x989D;&#x5916;&#x7684;&#x7279;&#x6027;&#x6765;&#x652F;&#x6301;&#x6807;&#x6CE8;&#x6216;&#x4FEE;&#x6539;&#x7C7B;&#x53CA;&#x5176;&#x6210;&#x5458;&#x3002;
&#x88C5;&#x9970;&#x5668;&#xFF08;Decorators&#xFF09;&#x4E3A;&#x6211;&#x4EEC;&#x5728;&#x7C7B;&#x7684;&#x58F0;&#x660E;&#x53CA;&#x6210;&#x5458;&#x4E0A;&#x901A;&#x8FC7;&#x5143;&#x7F16;&#x7A0B;&#x8BED;&#x6CD5;&#x6DFB;&#x52A0;&#x6807;&#x6CE8;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x3002;
Javascript&#x91CC;&#x7684;&#x88C5;&#x9970;&#x5668;&#x76EE;&#x524D;&#x5904;&#x5728;<a href="https://github.com/tc39/proposal-decorators" target="_blank">&#x5EFA;&#x8BAE;&#x5F81;&#x96C6;&#x7684;&#x7B2C;&#x4E8C;&#x9636;&#x6BB5;</a>&#xFF0C;&#x4F46;&#x5728;TypeScript&#x91CC;&#x5DF2;&#x505A;&#x4E3A;&#x4E00;&#x9879;&#x5B9E;&#x9A8C;&#x6027;&#x7279;&#x6027;&#x4E88;&#x4EE5;&#x652F;&#x6301;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x88C5;&#x9970;&#x5668;&#x662F;&#x4E00;&#x9879;&#x5B9E;&#x9A8C;&#x6027;&#x7279;&#x6027;&#xFF0C;&#x5728;&#x672A;&#x6765;&#x7684;&#x7248;&#x672C;&#x4E2D;&#x53EF;&#x80FD;&#x4F1A;&#x53D1;&#x751F;&#x6539;&#x53D8;&#x3002;</p>
</blockquote>
<p>&#x82E5;&#x8981;&#x542F;&#x7528;&#x5B9E;&#x9A8C;&#x6027;&#x7684;&#x88C5;&#x9970;&#x5668;&#x7279;&#x6027;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x6216;<code>tsconfig.json</code>&#x91CC;&#x542F;&#x7528;<code>experimentalDecorators</code>&#x7F16;&#x8BD1;&#x5668;&#x9009;&#x9879;&#xFF1A;</p>
<p><strong>&#x547D;&#x4EE4;&#x884C;</strong>:</p>
<pre><code class="lang-shell">tsc --target ES5 --experimentalDecorators
</code></pre>
<p><strong>tsconfig.json</strong>:</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
        <span class="hljs-string">&quot;target&quot;</span>: <span class="hljs-string">&quot;ES5&quot;</span>,
        <span class="hljs-string">&quot;experimentalDecorators&quot;</span>: <span class="hljs-literal">true</span>
    }
}
</code></pre>
<h1 id="&#x88C5;&#x9970;&#x5668;">&#x88C5;&#x9970;&#x5668;</h1>
<p><em>&#x88C5;&#x9970;&#x5668;</em>&#x662F;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7C7B;&#x578B;&#x7684;&#x58F0;&#x660E;&#xFF0C;&#x5B83;&#x80FD;&#x591F;&#x88AB;&#x9644;&#x52A0;&#x5230;<a href="#class-decorators">&#x7C7B;&#x58F0;&#x660E;</a>&#xFF0C;<a href="#method-decorators">&#x65B9;&#x6CD5;</a>&#xFF0C;<a href="#accessor-decorators">&#x8BBF;&#x95EE;&#x7B26;</a>&#xFF0C;<a href="#property-decorators">&#x5C5E;&#x6027;</a>&#x6216;<a href="#parameter-decorators">&#x53C2;&#x6570;</a>&#x4E0A;&#x3002;
&#x88C5;&#x9970;&#x5668;&#x4F7F;&#x7528;<code>@expression</code>&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;<code>expression</code>&#x6C42;&#x503C;&#x540E;&#x5FC5;&#x987B;&#x4E3A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x88AB;&#x88C5;&#x9970;&#x7684;&#x58F0;&#x660E;&#x4FE1;&#x606F;&#x505A;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;<code>@sealed</code>&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;<code>sealed</code>&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sealed</span>(<span class="hljs-params">target</span>) </span>{
    <span class="hljs-comment">// do something with &quot;target&quot; ...</span>
}
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x540E;&#x9762;<a href="#class-decorators">&#x7C7B;&#x88C5;&#x9970;&#x5668;</a>&#x5C0F;&#x8282;&#x91CC;&#x6709;&#x4E00;&#x4E2A;&#x66F4;&#x52A0;&#x8BE6;&#x7EC6;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
</blockquote>
<h2 id="&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;"><a name="decorator-factories"></a>&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;</h2>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x8981;&#x5B9A;&#x5236;&#x4E00;&#x4E2A;&#x4FEE;&#x9970;&#x5668;&#x5982;&#x4F55;&#x5E94;&#x7528;&#x5230;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x5F97;&#x5199;&#x4E00;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;&#x51FD;&#x6570;&#x3002;
<em>&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;</em>&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4EE5;&#x4F9B;&#x88C5;&#x9970;&#x5668;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5199;&#x4E00;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">color</span>(<span class="hljs-params">value: <span class="hljs-built_in">string</span></span>) </span>{ <span class="hljs-comment">// &#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">target</span>) </span>{ <span class="hljs-comment">//  &#x8FD9;&#x662F;&#x88C5;&#x9970;&#x5668;</span>
        <span class="hljs-comment">// do something with &quot;target&quot; and &quot;value&quot;...</span>
    }
}
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x4E0B;&#x9762;<a href="#method-decorators">&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;</a>&#x5C0F;&#x8282;&#x91CC;&#x6709;&#x4E00;&#x4E2A;&#x66F4;&#x52A0;&#x8BE6;&#x7EC6;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
</blockquote>
<h2 id="&#x88C5;&#x9970;&#x5668;&#x7EC4;&#x5408;">&#x88C5;&#x9970;&#x5668;&#x7EC4;&#x5408;</h2>
<p>&#x591A;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x5E94;&#x7528;&#x5230;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x4E0A;&#xFF0C;&#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<ul>
<li>&#x4E66;&#x5199;&#x5728;&#x540C;&#x4E00;&#x884C;&#x4E0A;&#xFF1A;</li>
</ul>
<pre><code class="lang-ts">@f @g x
</code></pre>
<ul>
<li>&#x4E66;&#x5199;&#x5728;&#x591A;&#x884C;&#x4E0A;&#xFF1A;</li>
</ul>
<pre><code class="lang-ts">@f
@g
x
</code></pre>
<p>&#x5F53;&#x591A;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x5E94;&#x7528;&#x4E8E;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x4E0A;&#xFF0C;&#x5B83;&#x4EEC;&#x6C42;&#x503C;&#x65B9;&#x5F0F;&#x4E0E;<a href="http://en.wikipedia.org/wiki/Function_composition" target="_blank">&#x590D;&#x5408;&#x51FD;&#x6570;</a>&#x76F8;&#x4F3C;&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x6A21;&#x578B;&#x4E0B;&#xFF0C;&#x5F53;&#x590D;&#x5408;<em>f</em>&#x548C;<em>g</em>&#x65F6;&#xFF0C;&#x590D;&#x5408;&#x7684;&#x7ED3;&#x679C;(<em>f</em> &#x2218; <em>g</em>)(<em>x</em>)&#x7B49;&#x540C;&#x4E8E;<em>f</em>(<em>g</em>(<em>x</em>))&#x3002;</p>
<p>&#x540C;&#x6837;&#x7684;&#xFF0C;&#x5728;TypeScript&#x91CC;&#xFF0C;&#x5F53;&#x591A;&#x4E2A;&#x88C5;&#x9970;&#x5668;&#x5E94;&#x7528;&#x5728;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x4E0A;&#x65F6;&#x4F1A;&#x8FDB;&#x884C;&#x5982;&#x4E0B;&#x6B65;&#x9AA4;&#x7684;&#x64CD;&#x4F5C;&#xFF1A;</p>
<ol>
<li>&#x7531;&#x4E0A;&#x81F3;&#x4E0B;&#x4F9D;&#x6B21;&#x5BF9;&#x88C5;&#x9970;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x6C42;&#x503C;&#x3002;</li>
<li>&#x6C42;&#x503C;&#x7684;&#x7ED3;&#x679C;&#x4F1A;&#x88AB;&#x5F53;&#x4F5C;&#x51FD;&#x6570;&#xFF0C;&#x7531;&#x4E0B;&#x81F3;&#x4E0A;&#x4F9D;&#x6B21;&#x8C03;&#x7528;&#x3002;</li>
</ol>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;<a href="#decorator-factories">&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;</a>&#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x6765;&#x89C2;&#x5BDF;&#x5B83;&#x4EEC;&#x6C42;&#x503C;&#x7684;&#x987A;&#x5E8F;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;f(): evaluated&quot;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">target, propertyKey: <span class="hljs-built_in">string</span>, descriptor: PropertyDescriptor</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;f(): called&quot;</span>);
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">g</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;g(): evaluated&quot;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">target, propertyKey: <span class="hljs-built_in">string</span>, descriptor: PropertyDescriptor</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;g(): called&quot;</span>);
    }
}

<span class="hljs-keyword">class</span> C {
    @f()
    @g()
    method() {}
}
</code></pre>
<p>&#x5728;&#x63A7;&#x5236;&#x53F0;&#x91CC;&#x4F1A;&#x6253;&#x5370;&#x51FA;&#x5982;&#x4E0B;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-shell">f(): evaluated
g(): evaluated
g(): called
f(): called
</code></pre>
<h2 id="&#x88C5;&#x9970;&#x5668;&#x6C42;&#x503C;">&#x88C5;&#x9970;&#x5668;&#x6C42;&#x503C;</h2>
<p>&#x7C7B;&#x4E2D;&#x4E0D;&#x540C;&#x58F0;&#x660E;&#x4E0A;&#x7684;&#x88C5;&#x9970;&#x5668;&#x5C06;&#x6309;&#x4EE5;&#x4E0B;&#x89C4;&#x5B9A;&#x7684;&#x987A;&#x5E8F;&#x5E94;&#x7528;&#xFF1A;</p>
<ol>
<li><em>&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;</em>&#xFF0C;&#x7136;&#x540E;&#x4F9D;&#x6B21;&#x662F;<em>&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;</em>&#xFF0C;<em>&#x8BBF;&#x95EE;&#x7B26;&#x88C5;&#x9970;&#x5668;</em>&#xFF0C;&#x6216;<em>&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;</em>&#x5E94;&#x7528;&#x5230;&#x6BCF;&#x4E2A;&#x5B9E;&#x4F8B;&#x6210;&#x5458;&#x3002;</li>
<li><em>&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;</em>&#xFF0C;&#x7136;&#x540E;&#x4F9D;&#x6B21;&#x662F;<em>&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;</em>&#xFF0C;<em>&#x8BBF;&#x95EE;&#x7B26;&#x88C5;&#x9970;&#x5668;</em>&#xFF0C;&#x6216;<em>&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;</em>&#x5E94;&#x7528;&#x5230;&#x6BCF;&#x4E2A;&#x9759;&#x6001;&#x6210;&#x5458;&#x3002;</li>
<li><em>&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;</em>&#x5E94;&#x7528;&#x5230;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;</li>
<li><em>&#x7C7B;&#x88C5;&#x9970;&#x5668;</em>&#x5E94;&#x7528;&#x5230;&#x7C7B;&#x3002;</li>
</ol>
<h2 id="&#x7C7B;&#x88C5;&#x9970;&#x5668;"><a name="class-decorators"></a>&#x7C7B;&#x88C5;&#x9970;&#x5668;</h2>
<p><em>&#x7C7B;&#x88C5;&#x9970;&#x5668;</em>&#x5728;&#x7C7B;&#x58F0;&#x660E;&#x4E4B;&#x524D;&#x88AB;&#x58F0;&#x660E;&#xFF08;&#x7D27;&#x9760;&#x7740;&#x7C7B;&#x58F0;&#x660E;&#xFF09;&#x3002;
&#x7C7B;&#x88C5;&#x9970;&#x5668;&#x5E94;&#x7528;&#x4E8E;&#x7C7B;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x76D1;&#x89C6;&#xFF0C;&#x4FEE;&#x6539;&#x6216;&#x66FF;&#x6362;&#x7C7B;&#x5B9A;&#x4E49;&#x3002;
&#x7C7B;&#x88C5;&#x9970;&#x5668;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x4E2D;(<code>.d.ts</code>)&#xFF0C;&#x4E5F;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x4EFB;&#x4F55;&#x5916;&#x90E8;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#xFF08;&#x6BD4;&#x5982;<code>declare</code>&#x7684;&#x7C7B;&#xFF09;&#x3002;</p>
<p>&#x7C7B;&#x88C5;&#x9970;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x5F53;&#x4F5C;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x5176;&#x552F;&#x4E00;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5982;&#x679C;&#x7C7B;&#x88C5;&#x9970;&#x5668;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x5B83;&#x4F1A;&#x4F7F;&#x7528;&#x63D0;&#x4F9B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x66FF;&#x6362;&#x7C7B;&#x7684;&#x58F0;&#x660E;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xA0; &#x5982;&#x679C;&#x4F60;&#x8981;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x6CE8;&#x610F;&#x5904;&#x7406;&#x597D;&#x539F;&#x6765;&#x7684;&#x539F;&#x578B;&#x94FE;&#x3002;
&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x88C5;&#x9970;&#x5668;&#x8C03;&#x7528;&#x903B;&#x8F91;&#x4E2D;<em>&#x4E0D;&#x4F1A;</em>&#x4E3A;&#x4F60;&#x505A;&#x8FD9;&#x4E9B;&#x3002;</p>
</blockquote>
<p>&#x4E0B;&#x9762;&#x662F;&#x4F7F;&#x7528;&#x7C7B;&#x88C5;&#x9970;&#x5668;(<code>@sealed</code>)&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5E94;&#x7528;&#x5728;<code>Greeter</code>&#x7C7B;&#xFF1A;</p>
<pre><code class="lang-ts">@sealed
<span class="hljs-keyword">class</span> Greeter {
    greeting: <span class="hljs-built_in">string</span>;
    <span class="hljs-keyword">constructor</span>(message: string) {
        <span class="hljs-keyword">this</span>.greeting = message;
    }
    greet() {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello, &quot;</span> + <span class="hljs-keyword">this</span>.greeting;
    }
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;<code>@sealed</code>&#x88C5;&#x9970;&#x5668;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sealed</span>(<span class="hljs-params">constructor: <span class="hljs-built_in">Function</span></span>) </span>{
    <span class="hljs-built_in">Object</span>.seal(<span class="hljs-keyword">constructor</span>);
    Object.seal(<span class="hljs-keyword">constructor</span>.prototype);
}
</code></pre>
<p>&#x5F53;<code>@sealed</code>&#x88AB;&#x6267;&#x884C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B83;&#x5C06;&#x5BC6;&#x5C01;&#x6B64;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;&#x539F;&#x578B;&#x3002;(&#x6CE8;&#xFF1A;&#x53C2;&#x89C1;<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/seal" target="_blank">Object.seal</a>)</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x91CD;&#x8F7D;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">classDecorator</span>&lt;<span class="hljs-title">T</span> <span class="hljs-title">extends</span> </span>{<span class="hljs-keyword">new</span>(...args:<span class="hljs-built_in">any</span>[]):{}}&gt;(<span class="hljs-keyword">constructor</span>:T) {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">class</span> extends <span class="hljs-keyword">constructor</span> {
        newProperty = <span class="hljs-string">&quot;new property&quot;</span>;
        hello = <span class="hljs-string">&quot;override&quot;</span>;
    }
}

@classDecorator
<span class="hljs-keyword">class</span> Greeter {
    property = <span class="hljs-string">&quot;property&quot;</span>;
    hello: <span class="hljs-built_in">string</span>;
    <span class="hljs-keyword">constructor</span>(m: string) {
        <span class="hljs-keyword">this</span>.hello = m;
    }
}

<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">new</span> Greeter(<span class="hljs-string">&quot;world&quot;</span>));
</code></pre>
<h2 id="&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;"><a name="method-decorators"></a>&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;</h2>
<p><em>&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;</em>&#x58F0;&#x660E;&#x5728;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x58F0;&#x660E;&#x4E4B;&#x524D;&#xFF08;&#x7D27;&#x9760;&#x7740;&#x65B9;&#x6CD5;&#x58F0;&#x660E;&#xFF09;&#x3002;
&#x5B83;&#x4F1A;&#x88AB;&#x5E94;&#x7528;&#x5230;&#x65B9;&#x6CD5;&#x7684;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x4E0A;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x76D1;&#x89C6;&#xFF0C;&#x4FEE;&#x6539;&#x6216;&#x8005;&#x66FF;&#x6362;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x3002;
&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x58F0;&#x660E;&#x6587;&#x4EF6;(<code>.d.ts</code>)&#xFF0C;&#x91CD;&#x8F7D;&#x6216;&#x8005;&#x4EFB;&#x4F55;&#x5916;&#x90E8;&#x4E0A;&#x4E0B;&#x6587;&#xFF08;&#x6BD4;&#x5982;<code>declare</code>&#x7684;&#x7C7B;&#xFF09;&#x4E2D;&#x3002;</p>
<p>&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x5F53;&#x4F5C;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x4F20;&#x5165;&#x4E0B;&#x5217;3&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<ol>
<li>&#x5BF9;&#x4E8E;&#x9759;&#x6001;&#x6210;&#x5458;&#x6765;&#x8BF4;&#x662F;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;&#x5B9E;&#x4F8B;&#x6210;&#x5458;&#x662F;&#x7C7B;&#x7684;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x6210;&#x5458;&#x7684;&#x540D;&#x5B57;&#x3002;</li>
<li>&#x6210;&#x5458;&#x7684;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x3002;</li>
</ol>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x5982;&#x679C;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x76EE;&#x6807;&#x7248;&#x672C;&#x5C0F;&#x4E8E;<code>ES5</code>&#xFF0C;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x5C06;&#x4F1A;&#x662F;<code>undefined</code>&#x3002;</p>
</blockquote>
<p>&#x5982;&#x679C;&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x5B83;&#x4F1A;&#x88AB;&#x7528;&#x4F5C;&#x65B9;&#x6CD5;&#x7684;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x5982;&#x679C;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x76EE;&#x6807;&#x7248;&#x672C;&#x5C0F;&#x4E8E;<code>ES5</code>&#x8FD4;&#x56DE;&#x503C;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#x3002;</p>
</blockquote>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x88C5;&#x9970;&#x5668;&#xFF08;<code>@enumerable</code>&#xFF09;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5E94;&#x7528;&#x4E8E;<code>Greeter</code>&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#x4E0A;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> Greeter {
    greeting: <span class="hljs-built_in">string</span>;
    <span class="hljs-keyword">constructor</span>(message: string) {
        <span class="hljs-keyword">this</span>.greeting = message;
    }

    @enumerable(<span class="hljs-literal">false</span>)
    greet() {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello, &quot;</span> + <span class="hljs-keyword">this</span>.greeting;
    }
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x6765;&#x5B9A;&#x4E49;<code>@enumerable</code>&#x88C5;&#x9970;&#x5668;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">enumerable</span>(<span class="hljs-params">value: <span class="hljs-built_in">boolean</span></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">target: <span class="hljs-built_in">any</span>, propertyKey: <span class="hljs-built_in">string</span>, descriptor: PropertyDescriptor</span>) </span>{
        descriptor.enumerable = value;
    };
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;<code>@enumerable(false)</code>&#x662F;&#x4E00;&#x4E2A;<a href="#decorator-factories">&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;</a>&#x3002;
&#x5F53;&#x88C5;&#x9970;&#x5668;<code>@enumerable(false)</code>&#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5B83;&#x4F1A;&#x4FEE;&#x6539;&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;&#x7684;<code>enumerable</code>&#x5C5E;&#x6027;&#x3002;</p>
<h2 id="&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;"><a name="accessor-decorators"></a>&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;</h2>
<p><em>&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;</em>&#x58F0;&#x660E;&#x5728;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x5668;&#x7684;&#x58F0;&#x660E;&#x4E4B;&#x524D;&#xFF08;&#x7D27;&#x9760;&#x7740;&#x8BBF;&#x95EE;&#x5668;&#x58F0;&#x660E;&#xFF09;&#x3002;
&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;&#x5E94;&#x7528;&#x4E8E;&#x8BBF;&#x95EE;&#x5668;&#x7684;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x76D1;&#x89C6;&#xFF0C;&#x4FEE;&#x6539;&#x6216;&#x66FF;&#x6362;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x5668;&#x7684;&#x5B9A;&#x4E49;&#x3002;
&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x4E2D;&#xFF08;.d.ts&#xFF09;&#xFF0C;&#x6216;&#x8005;&#x4EFB;&#x4F55;&#x5916;&#x90E8;&#x4E0A;&#x4E0B;&#x6587;&#xFF08;&#x6BD4;&#x5982;<code>declare</code>&#x7684;&#x7C7B;&#xFF09;&#x91CC;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; TypeScript&#x4E0D;&#x5141;&#x8BB8;&#x540C;&#x65F6;&#x88C5;&#x9970;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x7684;<code>get</code>&#x548C;<code>set</code>&#x8BBF;&#x95EE;&#x5668;&#x3002;&#x53D6;&#x800C;&#x4EE3;&#x4E4B;&#x7684;&#x662F;&#xFF0C;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x7684;&#x6240;&#x6709;&#x88C5;&#x9970;&#x7684;&#x5FC5;&#x987B;&#x5E94;&#x7528;&#x5728;&#x6587;&#x6863;&#x987A;&#x5E8F;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x5668;&#x4E0A;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#xFF0C;&#x5728;&#x88C5;&#x9970;&#x5668;&#x5E94;&#x7528;&#x4E8E;&#x4E00;&#x4E2A;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x65F6;&#xFF0C;&#x5B83;&#x8054;&#x5408;&#x4E86;<code>get</code>&#x548C;<code>set</code>&#x8BBF;&#x95EE;&#x5668;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5206;&#x5F00;&#x58F0;&#x660E;&#x7684;&#x3002;</p>
</blockquote>
<p>&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x5F53;&#x4F5C;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x4F20;&#x5165;&#x4E0B;&#x5217;3&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<ol>
<li>&#x5BF9;&#x4E8E;&#x9759;&#x6001;&#x6210;&#x5458;&#x6765;&#x8BF4;&#x662F;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;&#x5B9E;&#x4F8B;&#x6210;&#x5458;&#x662F;&#x7C7B;&#x7684;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x6210;&#x5458;&#x7684;&#x540D;&#x5B57;&#x3002;</li>
<li>&#x6210;&#x5458;&#x7684;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x3002;</li>
</ol>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x5982;&#x679C;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x76EE;&#x6807;&#x7248;&#x672C;&#x5C0F;&#x4E8E;<code>ES5</code>&#xFF0C;<em>Property Descriptor</em>&#x5C06;&#x4F1A;&#x662F;<code>undefined</code>&#x3002;</p>
</blockquote>
<p>&#x5982;&#x679C;&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x5B83;&#x4F1A;&#x88AB;&#x7528;&#x4F5C;&#x65B9;&#x6CD5;&#x7684;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x5982;&#x679C;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x76EE;&#x6807;&#x7248;&#x672C;&#x5C0F;&#x4E8E;<code>ES5</code>&#x8FD4;&#x56DE;&#x503C;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#x3002;</p>
</blockquote>
<p>&#x4E0B;&#x9762;&#x662F;&#x4F7F;&#x7528;&#x4E86;&#x8BBF;&#x95EE;&#x5668;&#x88C5;&#x9970;&#x5668;&#xFF08;<code>@configurable</code>&#xFF09;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5E94;&#x7528;&#x4E8E;<code>Point</code>&#x7C7B;&#x7684;&#x6210;&#x5458;&#x4E0A;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> Point {
    <span class="hljs-keyword">private</span> _x: <span class="hljs-built_in">number</span>;
    <span class="hljs-keyword">private</span> _y: <span class="hljs-built_in">number</span>;
    <span class="hljs-keyword">constructor</span>(x: number, y: number) {
        <span class="hljs-keyword">this</span>._x = x;
        <span class="hljs-keyword">this</span>._y = y;
    }

    @configurable(<span class="hljs-literal">false</span>)
    <span class="hljs-keyword">get</span> x() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._x; }

    @configurable(<span class="hljs-literal">false</span>)
    <span class="hljs-keyword">get</span> y() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._y; }
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5982;&#x4E0B;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x6765;&#x5B9A;&#x4E49;<code>@configurable</code>&#x88C5;&#x9970;&#x5668;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">configurable</span>(<span class="hljs-params">value: <span class="hljs-built_in">boolean</span></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">target: <span class="hljs-built_in">any</span>, propertyKey: <span class="hljs-built_in">string</span>, descriptor: PropertyDescriptor</span>) </span>{
        descriptor.configurable = value;
    };
}
</code></pre>
<h2 id="&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;"><a name="property-decorators"></a>&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;</h2>
<p><em>&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;</em>&#x58F0;&#x660E;&#x5728;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x58F0;&#x660E;&#x4E4B;&#x524D;&#xFF08;&#x7D27;&#x9760;&#x7740;&#x5C5E;&#x6027;&#x58F0;&#x660E;&#xFF09;&#x3002;
&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x4E2D;&#xFF08;.d.ts&#xFF09;&#xFF0C;&#x6216;&#x8005;&#x4EFB;&#x4F55;&#x5916;&#x90E8;&#x4E0A;&#x4E0B;&#x6587;&#xFF08;&#x6BD4;&#x5982;<code>declare</code>&#x7684;&#x7C7B;&#xFF09;&#x91CC;&#x3002;</p>
<p>&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x5F53;&#x4F5C;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x4F20;&#x5165;&#x4E0B;&#x5217;2&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<ol>
<li>&#x5BF9;&#x4E8E;&#x9759;&#x6001;&#x6210;&#x5458;&#x6765;&#x8BF4;&#x662F;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;&#x5B9E;&#x4F8B;&#x6210;&#x5458;&#x662F;&#x7C7B;&#x7684;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x6210;&#x5458;&#x7684;&#x540D;&#x5B57;&#x3002;</li>
</ol>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; <em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x4E0D;&#x4F1A;&#x505A;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x8FD9;&#x4E0E;TypeScript&#x662F;&#x5982;&#x4F55;&#x521D;&#x59CB;&#x5316;&#x5C5E;&#x6027;&#x88C5;&#x9970;&#x5668;&#x7684;&#x6709;&#x5173;&#x3002;
&#x56E0;&#x4E3A;&#x76EE;&#x524D;&#x6CA1;&#x6709;&#x529E;&#x6CD5;&#x5728;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x65F6;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x6CA1;&#x529E;&#x6CD5;&#x76D1;&#x89C6;&#x6216;&#x4FEE;&#x6539;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x521D;&#x59CB;&#x5316;&#x65B9;&#x6CD5;&#x3002;&#x8FD4;&#x56DE;&#x503C;&#x4E5F;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#x3002;
&#x56E0;&#x6B64;&#xFF0C;&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;&#x53EA;&#x80FD;&#x7528;&#x6765;&#x76D1;&#x89C6;&#x7C7B;&#x4E2D;&#x662F;&#x5426;&#x58F0;&#x660E;&#x4E86;&#x67D0;&#x4E2A;&#x540D;&#x5B57;&#x7684;&#x5C5E;&#x6027;&#x3002;</p>
</blockquote>
<p>&#x5982;&#x679C;&#x8BBF;&#x95EE;&#x7B26;&#x88C5;&#x9970;&#x5668;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x503C;&#xFF0C;&#x5B83;&#x4F1A;&#x88AB;&#x7528;&#x4F5C;&#x65B9;&#x6CD5;&#x7684;<em>&#x5C5E;&#x6027;&#x63CF;&#x8FF0;&#x7B26;</em>&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x5B83;&#x6765;&#x8BB0;&#x5F55;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x5143;&#x6570;&#x636E;&#xFF0C;&#x5982;&#x4E0B;&#x4F8B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> Greeter {
    @format(<span class="hljs-string">&quot;Hello, %s&quot;</span>)
    greeting: <span class="hljs-built_in">string</span>;

    <span class="hljs-keyword">constructor</span>(message: string) {
        <span class="hljs-keyword">this</span>.greeting = message;
    }
    greet() {
        <span class="hljs-keyword">let</span> formatString = getFormat(<span class="hljs-keyword">this</span>, <span class="hljs-string">&quot;greeting&quot;</span>);
        <span class="hljs-keyword">return</span> formatString.replace(<span class="hljs-string">&quot;%s&quot;</span>, <span class="hljs-keyword">this</span>.greeting);
    }
}
</code></pre>
<p>&#x7136;&#x540E;&#x5B9A;&#x4E49;<code>@format</code>&#x88C5;&#x9970;&#x5668;&#x548C;<code>getFormat</code>&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;reflect-metadata&quot;</span>;

<span class="hljs-keyword">const</span> formatMetadataKey = Symbol(<span class="hljs-string">&quot;format&quot;</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">format</span>(<span class="hljs-params">formatString: <span class="hljs-built_in">string</span></span>) </span>{
    <span class="hljs-keyword">return</span> Reflect.metadata(formatMetadataKey, formatString);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getFormat</span>(<span class="hljs-params">target: <span class="hljs-built_in">any</span>, propertyKey: <span class="hljs-built_in">string</span></span>) </span>{
    <span class="hljs-keyword">return</span> Reflect.getMetadata(formatMetadataKey, target, propertyKey);
}
</code></pre>
<p>&#x8FD9;&#x4E2A;<code>@format(&quot;Hello, %s&quot;)</code>&#x88C5;&#x9970;&#x5668;&#x662F;&#x4E2A; <a href="#decorator-factories">&#x88C5;&#x9970;&#x5668;&#x5DE5;&#x5382;</a>&#x3002;
&#x5F53;<code>@format(&quot;Hello, %s&quot;)</code>&#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5B83;&#x6DFB;&#x52A0;&#x4E00;&#x6761;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x5143;&#x6570;&#x636E;&#xFF0C;&#x901A;&#x8FC7;<code>reflect-metadata</code>&#x5E93;&#x91CC;&#x7684;<code>Reflect.metadata</code>&#x51FD;&#x6570;&#x3002;
&#x5F53;<code>getFormat</code>&#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5B83;&#x8BFB;&#x53D6;&#x683C;&#x5F0F;&#x7684;&#x5143;&#x6570;&#x636E;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>reflect-metadata</code>&#x5E93;&#x3002;
&#x67E5;&#x770B;<a href="#metadata">&#x5143;&#x6570;&#x636E;</a>&#x4E86;&#x89E3;<code>reflect-metadata</code>&#x5E93;&#x66F4;&#x8BE6;&#x7EC6;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
</blockquote>
<h2 id="&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;"><a name="parameter-decorators"></a>&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;</h2>
<p><em>&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;</em>&#x58F0;&#x660E;&#x5728;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x58F0;&#x660E;&#x4E4B;&#x524D;&#xFF08;&#x7D27;&#x9760;&#x7740;&#x53C2;&#x6570;&#x58F0;&#x660E;&#xFF09;&#x3002;
&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;&#x5E94;&#x7528;&#x4E8E;&#x7C7B;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6216;&#x65B9;&#x6CD5;&#x58F0;&#x660E;&#x3002;
&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#xFF08;.d.ts&#xFF09;&#xFF0C;&#x91CD;&#x8F7D;&#x6216;&#x5176;&#x5B83;&#x5916;&#x90E8;&#x4E0A;&#x4E0B;&#x6587;&#xFF08;&#x6BD4;&#x5982;<code>declare</code>&#x7684;&#x7C7B;&#xFF09;&#x91CC;&#x3002;</p>
<p>&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x5F53;&#x4F5C;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x4F20;&#x5165;&#x4E0B;&#x5217;3&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<ol>
<li>&#x5BF9;&#x4E8E;&#x9759;&#x6001;&#x6210;&#x5458;&#x6765;&#x8BF4;&#x662F;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;&#x5B9E;&#x4F8B;&#x6210;&#x5458;&#x662F;&#x7C7B;&#x7684;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x3002;</li>
<li>&#x6210;&#x5458;&#x7684;&#x540D;&#x5B57;&#x3002;</li>
<li>&#x53C2;&#x6570;&#x5728;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x7D22;&#x5F15;&#x3002;</li>
</ol>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;&#x53EA;&#x80FD;&#x7528;&#x6765;&#x76D1;&#x89C6;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x662F;&#x5426;&#x88AB;&#x4F20;&#x5165;&#x3002;</p>
</blockquote>
<p>&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#x3002;</p>
<p>&#x4E0B;&#x4F8B;&#x5B9A;&#x4E49;&#x4E86;&#x53C2;&#x6570;&#x88C5;&#x9970;&#x5668;&#xFF08;<code>@required</code>&#xFF09;&#x5E76;&#x5E94;&#x7528;&#x4E8E;<code>Greeter</code>&#x7C7B;&#x65B9;&#x6CD5;&#x7684;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> Greeter {
    greeting: <span class="hljs-built_in">string</span>;

    <span class="hljs-keyword">constructor</span>(message: string) {
        <span class="hljs-keyword">this</span>.greeting = message;
    }

    @validate
    greet(@required name: <span class="hljs-built_in">string</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello &quot;</span> + name + <span class="hljs-string">&quot;, &quot;</span> + <span class="hljs-keyword">this</span>.greeting;
    }
}
</code></pre>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x51FD;&#x6570;&#x5B9A;&#x4E49; <code>@required</code> &#x548C; <code>@validate</code> &#x88C5;&#x9970;&#x5668;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;reflect-metadata&quot;</span>;

<span class="hljs-keyword">const</span> requiredMetadataKey = Symbol(<span class="hljs-string">&quot;required&quot;</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">required</span>(<span class="hljs-params">target: <span class="hljs-built_in">Object</span>, propertyKey: <span class="hljs-built_in">string</span> | symbol, parameterIndex: <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-keyword">let</span> existingRequiredParameters: <span class="hljs-built_in">number</span>[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyKey) || [];
    existingRequiredParameters.push(parameterIndex);
    Reflect.defineMetadata(requiredMetadataKey, existingRequiredParameters, target, propertyKey);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">validate</span>(<span class="hljs-params">target: <span class="hljs-built_in">any</span>, propertyName: <span class="hljs-built_in">string</span>, descriptor: TypedPropertyDescriptor&lt;<span class="hljs-built_in">Function</span>&gt;</span>) </span>{
    <span class="hljs-keyword">let</span> method = descriptor.value;
    descriptor.value = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">let</span> requiredParameters: <span class="hljs-built_in">number</span>[] = Reflect.getOwnMetadata(requiredMetadataKey, target, propertyName);
        <span class="hljs-keyword">if</span> (requiredParameters) {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> parameterIndex of requiredParameters) {
                <span class="hljs-keyword">if</span> (parameterIndex &gt;= <span class="hljs-built_in">arguments</span>.length || <span class="hljs-built_in">arguments</span>[parameterIndex] === <span class="hljs-literal">undefined</span>) {
                    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">&quot;Missing required argument.&quot;</span>);
                }
            }
        }

        <span class="hljs-keyword">return</span> method.apply(<span class="hljs-keyword">this</span>, <span class="hljs-built_in">arguments</span>);
    }
}
</code></pre>
<p><code>@required</code>&#x88C5;&#x9970;&#x5668;&#x6DFB;&#x52A0;&#x4E86;&#x5143;&#x6570;&#x636E;&#x5B9E;&#x4F53;&#x628A;&#x53C2;&#x6570;&#x6807;&#x8BB0;&#x4E3A;&#x5FC5;&#x9700;&#x7684;&#x3002;
<code>@validate</code>&#x88C5;&#x9970;&#x5668;&#x628A;<code>greet</code>&#x65B9;&#x6CD5;&#x5305;&#x88F9;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x91CC;&#x5728;&#x8C03;&#x7528;&#x539F;&#x5148;&#x7684;&#x51FD;&#x6570;&#x524D;&#x9A8C;&#x8BC1;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4F7F;&#x7528;&#x4E86;<code>reflect-metadata</code>&#x5E93;&#x3002;
&#x67E5;&#x770B;<a href="#metadata">&#x5143;&#x6570;&#x636E;</a>&#x4E86;&#x89E3;<code>reflect-metadata</code>&#x5E93;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x3002;</p>
</blockquote>
<h2 id="&#x5143;&#x6570;&#x636E;">&#x5143;&#x6570;&#x636E;</h2>
<p>&#x4E00;&#x4E9B;&#x4F8B;&#x5B50;&#x4F7F;&#x7528;&#x4E86;<code>reflect-metadata</code>&#x5E93;&#x6765;&#x652F;&#x6301;<a href="https://github.com/rbuckton/ReflectDecorators" target="_blank">&#x5B9E;&#x9A8C;&#x6027;&#x7684;metadata API</a>&#x3002;
&#x8FD9;&#x4E2A;&#x5E93;&#x8FD8;&#x4E0D;&#x662F;ECMAScript (JavaScript)&#x6807;&#x51C6;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x3002;
&#x7136;&#x800C;&#xFF0C;&#x5F53;&#x88C5;&#x9970;&#x5668;&#x88AB;ECMAScript&#x5B98;&#x65B9;&#x6807;&#x51C6;&#x91C7;&#x7EB3;&#x540E;&#xFF0C;&#x8FD9;&#x4E9B;&#x6269;&#x5C55;&#x4E5F;&#x5C06;&#x88AB;&#x63A8;&#x8350;&#x7ED9;ECMAScript&#x4EE5;&#x91C7;&#x7EB3;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;npm&#x5B89;&#x88C5;&#x8FD9;&#x4E2A;&#x5E93;&#xFF1A;</p>
<pre><code class="lang-shell">npm i reflect-metadata --save
</code></pre>
<p>TypeScript&#x652F;&#x6301;&#x4E3A;&#x5E26;&#x6709;&#x88C5;&#x9970;&#x5668;&#x7684;&#x58F0;&#x660E;&#x751F;&#x6210;&#x5143;&#x6570;&#x636E;&#x3002;
&#x4F60;&#x9700;&#x8981;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x6216;<code>tsconfig.json</code>&#x91CC;&#x542F;&#x7528;<code>emitDecoratorMetadata</code>&#x7F16;&#x8BD1;&#x5668;&#x9009;&#x9879;&#x3002;</p>
<p><strong>Command Line</strong>:</p>
<pre><code class="lang-shell">tsc --target ES5 --experimentalDecorators --emitDecoratorMetadata
</code></pre>
<p><strong>tsconfig.json</strong>:</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
        <span class="hljs-string">&quot;target&quot;</span>: <span class="hljs-string">&quot;ES5&quot;</span>,
        <span class="hljs-string">&quot;experimentalDecorators&quot;</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">&quot;emitDecoratorMetadata&quot;</span>: <span class="hljs-literal">true</span>
    }
}
</code></pre>
<p>&#x5F53;&#x542F;&#x7528;&#x540E;&#xFF0C;&#x53EA;&#x8981;<code>reflect-metadata</code>&#x5E93;&#x88AB;&#x5F15;&#x5165;&#x4E86;&#xFF0C;&#x8BBE;&#x8BA1;&#x9636;&#x6BB5;&#x6DFB;&#x52A0;&#x7684;&#x7C7B;&#x578B;&#x4FE1;&#x606F;&#x53EF;&#x4EE5;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x5982;&#x4E0B;&#x4F8B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;reflect-metadata&quot;</span>;

<span class="hljs-keyword">class</span> Point {
    x: <span class="hljs-built_in">number</span>;
    y: <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">class</span> Line {
    <span class="hljs-keyword">private</span> _p0: Point;
    <span class="hljs-keyword">private</span> _p1: Point;

    @validate
    <span class="hljs-keyword">set</span> p0(value: Point) { <span class="hljs-keyword">this</span>._p0 = value; }
    <span class="hljs-keyword">get</span> p0() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._p0; }

    @validate
    <span class="hljs-keyword">set</span> p1(value: Point) { <span class="hljs-keyword">this</span>._p1 = value; }
    <span class="hljs-keyword">get</span> p1() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._p1; }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">validate</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">target: <span class="hljs-built_in">any</span>, propertyKey: <span class="hljs-built_in">string</span>, descriptor: TypedPropertyDescriptor&lt;T&gt;</span>) </span>{
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">set</span> = descriptor.set;
    descriptor.set = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value: T</span>) </span>{
        <span class="hljs-keyword">let</span> <span class="hljs-keyword">type</span> = Reflect.getMetadata(<span class="hljs-string">&quot;design:type&quot;</span>, target, propertyKey);
        <span class="hljs-keyword">if</span> (!(value <span class="hljs-keyword">instanceof</span> <span class="hljs-keyword">type</span>)) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">&quot;Invalid type.&quot;</span>);
        }
        <span class="hljs-keyword">set</span>(value);
    }
}
</code></pre>
<p>TypeScript&#x7F16;&#x8BD1;&#x5668;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>@Reflect.metadata</code>&#x88C5;&#x9970;&#x5668;&#x6CE8;&#x5165;&#x8BBE;&#x8BA1;&#x9636;&#x6BB5;&#x7684;&#x7C7B;&#x578B;&#x4FE1;&#x606F;&#x3002;
&#x4F60;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x5B83;&#x76F8;&#x5F53;&#x4E8E;&#x4E0B;&#x9762;&#x7684;TypeScript&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">class</span> Line {
    <span class="hljs-keyword">private</span> _p0: Point;
    <span class="hljs-keyword">private</span> _p1: Point;

    @validate
    @Reflect.metadata(<span class="hljs-string">&quot;design:type&quot;</span>, Point)
    <span class="hljs-keyword">set</span> p0(value: Point) { <span class="hljs-keyword">this</span>._p0 = value; }
    <span class="hljs-keyword">get</span> p0() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._p0; }

    @validate
    @Reflect.metadata(<span class="hljs-string">&quot;design:type&quot;</span>, Point)
    <span class="hljs-keyword">set</span> p1(value: Point) { <span class="hljs-keyword">this</span>._p1 = value; }
    <span class="hljs-keyword">get</span> p1() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._p1; }
}
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#x2003; &#x88C5;&#x9970;&#x5668;&#x5143;&#x6570;&#x636E;&#x662F;&#x4E2A;&#x5B9E;&#x9A8C;&#x6027;&#x7684;&#x7279;&#x6027;&#x5E76;&#x4E14;&#x53EF;&#x80FD;&#x5728;&#x4EE5;&#x540E;&#x7684;&#x7248;&#x672C;&#x4E2D;&#x53D1;&#x751F;&#x7834;&#x574F;&#x6027;&#x7684;&#x6539;&#x53D8;&#xFF08;breaking changes&#xFF09;&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="JSX.html" class="navigation navigation-prev " aria-label="Previous page: JSX">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Mixins.html" class="navigation navigation-next " aria-label="Next page: 混入">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Decorators","level":"1.3.21","depth":2,"next":{"title":"混入","level":"1.3.22","depth":2,"path":"doc/handbook/Mixins.md","ref":"./doc/handbook/Mixins.md","articles":[]},"previous":{"title":"JSX","level":"1.3.20","depth":2,"path":"doc/handbook/JSX.md","ref":"./doc/handbook/JSX.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Patrick Zhong","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"preface.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"TypeScript Handbook","language":"zh-hans","gitbook":">3.0.0","description":"Translation of official TypeScript Handbook"},"file":{"path":"doc/handbook/Decorators.md","mtime":"2018-10-13T09:48:54.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-13T09:57:42.904Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

